package com.sunwayhorizo.health.business.clinic.orders.query;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.Stateless;

import org.jboss.seam.annotations.Name;

import com.sunwayhorizo.health.business.base.query.BaseQuery;
import com.sunwayhorizo.health.common.ListUtil;
import com.sunwayhorizo.health.common.StrUtil;
import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.rim.Act;
import com.sunwayhorizo.health.hl7.rim.InvoiceElement;
import com.sunwayhorizo.health.hl7.rim.Observation;
import com.sunwayhorizo.health.hl7.rim.Procedure;
import com.sunwayhorizo.health.hl7.rim.SubstanceAdministration;
import com.sunwayhorizo.health.hl7.vocabulary.ActClass;
import com.sunwayhorizo.health.hl7.vocabulary.ActCode;
import com.sunwayhorizo.health.hl7.vocabulary.ActMood;

@Name("OrdersQuery")
@Stateless
public class OrdersQuery extends BaseQuery implements OrdersQueryLocal
{

    @SuppressWarnings("unchecked")
    public List<InvoiceElement> findInvoiceElementByCode(String code,int pageNo, int pageSize) throws Hl7Exception
    {
        conditions.clear();
        
        Map<String,Object> inve = new HashMap<String,Object>();
        inve.put("classCode", ActClass.INVE.code().toString());
        if(StrUtil.isNotEmpty(code))
        {
            inve.put("codeEtsId", code);
        }
        
        conditions.put("act", inve);
        
        Map<String,Object> inveIi = new HashMap<String,Object>();
        conditions.put("aii", inveIi);
        
        Map<String,Object> orderBy = new HashMap<String,Object>();
        orderBy.put("asc", "aii.extensionTxt");
        conditions.put("order by", orderBy);
        
        List<InvoiceElement> inves =  (List<InvoiceElement>) queryResults(conditions,InvoiceElement.class,pageNo,pageSize);
        
        return inves;
        
    }
    
    public long findInvoiceElementByCode(String code) throws Hl7Exception
    {
        conditions.clear();
        Map<String,Object> inve = new HashMap<String,Object>();
        inve.put("classCode", ActClass.INVE.code().toString());
        if(StrUtil.isNotEmpty(code))
        {
            inve.put("codeEtsId", code);
        }
        
        conditions.put("act", inve);
        
        long countInves =  queryCountResults(conditions,InvoiceElement.class);
        
        return countInves;
        
    }
    
    @SuppressWarnings("unchecked")
    public List<SubstanceAdministration> findSubstanceAdministrationByCond(List<String> materialclassCodes,int pageNo, int pageSize) throws Hl7Exception
    {
        if(ListUtil.isEmpty(materialclassCodes)) return null;
        conditions.clear();
       
        Map<String,Object> subAdmt = new HashMap<String,Object>();
        subAdmt.put("classCode", ActClass.SBADM.code().toString());
        subAdmt.put("codeEtsId", ActCode.DRUG.code().toString());
        subAdmt.put("levelCodeEtsId", materialclassCodes);
        
        conditions.put("act", subAdmt);
        
        
        List<SubstanceAdministration> subAdmts =  (List<SubstanceAdministration>) queryResults(conditions,SubstanceAdministration.class,pageNo,pageSize);
        return subAdmts;
    }


    @Override
    public long findSubstanceAdministrationByCond(List<String> materialclassCodes) throws Hl7Exception
    {
        conditions.clear();
        
        Map<String,Object> subAdmt = new HashMap<String,Object>();
        subAdmt.put("classCode", ActClass.SBADM.code().toString());
        subAdmt.put("codeEtsId", ActCode.DRUG.code().toString());
        subAdmt.put("levelCodeEtsId", materialclassCodes);
        
        conditions.put("act", subAdmt);
        
        
        long countsubAdmts =  queryCountResults(conditions,SubstanceAdministration.class);
        return countsubAdmts;
    }
    
    public List<? extends Act> findCountObsOrProcByCond(String code,String ywm,int pageNo, int pageSize) throws Hl7Exception
    {
        if(StrUtil.isEmpty(code)) return null;
        conditions.clear();
       
        Map<String,Object> obsOrProc = new HashMap<String,Object>();
        obsOrProc.put("mood_Code", ActMood.DEF.code().toString());
        obsOrProc.put("codeEtsId", code);
        
        conditions.put("act", obsOrProc);
        List<? extends Act> obsOrProcList  = null;
        if(code.equals(ActCode.EXAM.code().toString()) || code.equals(ActCode.LAB.code().toString()))
        {
            obsOrProcList =  (List<? extends Act>) queryResults(conditions,Observation.class,pageNo,pageSize);
        }
        else
        {
             obsOrProcList =  (List<? extends Act>) queryResults(conditions,Procedure.class,pageNo,pageSize);
        }
        
        return obsOrProcList;
    }
    
    public long findObsOrProcByCond(String code) throws Hl7Exception
    {
        if(StrUtil.isEmpty(code)) return 0;
        conditions.clear();
       
        Map<String,Object> obsOrProc = new HashMap<String,Object>();
        obsOrProc.put("mood_Code", ActMood.DEF.code().toString());
        obsOrProc.put("codeEtsId", code);
        
        conditions.put("act", obsOrProc);
        long countInves = 0;
        if(code.equals(ActCode.EXAM.code().toString()) || code.equals(ActCode.LAB.code().toString()))
        {
             countInves =  queryCountResults(conditions,Observation.class);
        }
        else
        {
            countInves =  queryCountResults(conditions,Procedure.class);
        }
        
        return countInves;
    }
    
    
}
